package com.axc.gallery2.dbhelper;

import java.io.File;
import java.text.MessageFormat;

import android.content.Context;
import android.database.Cursor;

public class ImagesDBhelper extends DbHelper {

	public ImagesDBhelper(Context context) {
		super(context);
	}

	// 删除无效的数据
	public void checkImages(){
		String sql = MessageFormat.format("SELECT * FROM {0}", TABLE_IMAGES_NAME);
		System.out.println("sql:"+sql);
		Cursor cursor = rawQuery(sql,null);
		String name;
		File file;
		boolean isExists;
		int locationColumn;
		int colunmid;
		System.out.println("cursor:"+cursor.getColumnCount());
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
		{
			System.out.println("cursor.getPosition:"+cursor.getPosition());
			locationColumn = cursor.getColumnIndex(TABLE_IMAGES_COLUNM_LOCATION);
			name = cursor.getString(locationColumn);
			colunmid = cursor.getColumnIndex(FIELD_ID);

			file =new File(name);
		
			isExists =  file.exists();
			System.out.println("isExists:"+isExists);
			if(!isExists)
			{
				super.delete(TABLE_IMAGES_NAME, cursor.getInt(colunmid));
				System.out.println("delete");
			}
			System.out.println("complete:"+cursor.getPosition());
		};
		cursor.close();
		super.colseDBbase();
	}

	//添加记录
	public void insertImage(String location,int albumId){
		System.out.println("insertimagesing...");

		String sqlString= MessageFormat.format("SELECT COUNT(1) FROM {0} WHERE {1}={2} AND {3}={4}",
				TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_LOCATION,"'"+location+"'",TABLE_IMAGES_COLUNM_ALBUMID,"'"+albumId+"'");
		System.out.println(sqlString);
		Cursor cursor = this.rawQuery(sqlString, null);
		cursor.moveToFirst();
		if(0 == cursor.getInt(0))
		{
			System.out.println("insert");
			super.insert(TABLE_IMAGES_NAME, new String[]{TABLE_IMAGES_COLUNM_LOCATION,TABLE_IMAGES_COLUNM_ALBUMID}
			, new String[]{location,String.valueOf(albumId)});
			System.out.println("insertcomplete");
		}
		cursor.close();
		super.colseDBbase();
	}
	//删除记录
	public void deleteImage(int id){
		super.delete(DbHelper.TABLE_IMAGES_NAME, id);
	}
	public void deleteImage(int albumID,String location){
		String sqlString =MessageFormat.format("DELETE FROM {0} WHERE {1}={2} AND {3}={4}",
				TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_LOCATION,"'"+location+"'",TABLE_IMAGES_COLUNM_ALBUMID,"'"+albumID+"'");
		this.excuteSql(sqlString);
	}
	public void deleteImage(String location){
		String sqlString =MessageFormat.format("DELETE FROM {0} WHERE {1}={2}",
				TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_LOCATION,"'"+location+"'");
		this.excuteSql(sqlString);
	}
	//修改记录
	public void updeteImage(int id,String[] columns,String[] values){
		super.update(TABLE_IMAGES_NAME, id, columns, values);
	}
	//获取所属相册名称
	public String[] getImageAlbums(String location){
		String sqlString = MessageFormat.format("SELECT {0} FROM {1} WHERE {2} IN (SELECT {2} FROM {3} WHERE {4}=? GROUP BY _id"
				,TABLE_ALBUM_COLUNM_NAME,TABLE_ALBUM_NAME,FIELD_ID,TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_LOCATION);
		Cursor cursor = getWritableDatabase().rawQuery(sqlString,new String[]{ location});
		String[] imageAlbums =new String[cursor.getCount()];
		int i =0;
		for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext())
		{

			imageAlbums[i] = cursor.getString(0);
			i++;

		};
		cursor.close();
		super.colseDBbase();
		return imageAlbums;
	}

	public Cursor rawQuery( String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		return super.rawQuery(TABLE_IMAGES_NAME, columns, selection, selectionArgs, groupBy,
				having, orderBy);
	}
	//删除图片
	public void delByAllbumId(int albumId) {
		String sql = MessageFormat.format("DELETE FROM {0} WHERE {1}={2}", TABLE_IMAGES_NAME,TABLE_IMAGES_COLUNM_ALBUMID,albumId);
		super.excuteSql(sql);
	}


}
